package com.swiftops.basic.login.service.impl;

import com.swiftops.basic.login.dao.SysLoginMapper;
import com.swiftops.basic.login.service.SysLoginService;
import com.swiftops.basic.login.vo.entity.SysLoginEntityVO;
import com.swiftops.basic.login.vo.response.SysLoginResponseVO;
import com.swiftops.basic.sys.dao.SysUserMapper;
import com.swiftops.basic.sys.model.SysDict;
import com.swiftops.basic.sys.model.SysUser;
import com.swiftops.basic.sys.service.SysDictService;
import com.swiftops.basic.sys.vo.entity.SysUserEntityVO;
import com.swiftops.common.model.TokenModel;
import com.swiftops.common.model.User;
import com.swiftops.common.service.AbstractService;
import com.swiftops.common.service.TokenService;
import com.swiftops.common.util.JasyptUtils;
import com.swiftops.common.util.ListUtil;
import com.swiftops.common.vo.response.DictResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 用户信息
 *
 * @author yangyang
 * @date 2023/09/17
 **/
@Slf4j
@Service
@Transactional
public class SysLoginServiceImpl extends AbstractService<SysUser> implements SysLoginService {

    @Resource
    private SysLoginMapper sysLoginMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private SysDictService sysDictService;

    /**
     * 获取用户信息详细信息
     *
     * @param id 用户信息ID
     * @return 用户信息
     */
    @Override
    public SysUserEntityVO getSysUserEntityVO(String id) {
        SysUser sysUser = this.selectByPrimaryKey(id);
        if (null == sysUser) return null;

        SysUserEntityVO sysUserEntityVO = new SysUserEntityVO();
        BeanUtils.copyProperties(sysUser, sysUserEntityVO);
        return sysUserEntityVO;
    }

    /**
     * 添加用户信息
     *
     * @param entityVO 用户信息
     */
    @Override
    public TokenModel login(SysLoginEntityVO entityVO) throws Exception {
        SysUser selectOne = this.selectOne(new SysUser(entityVO.getUsername()));
        if (selectOne == null) throw new Exception("用户名密码错误");
        String password = selectOne.getPassword();
        boolean b = JasyptUtils.checkPassword(entityVO.getPassword(), password);
        if (!b) throw new Exception("用户名密码错误");

        SysLoginResponseVO responseVO = new SysLoginResponseVO();
        User user = new User();
        BeanUtils.copyProperties(selectOne, user);
        user.setUserId(selectOne.getId());

        List<String> roleList = sysLoginMapper.getRoleNameByUserId(selectOne.getId());
        user.setRoleList(roleList);
        TokenModel tokenModel = tokenService.createToken(user);
        //
        List<SysDict> sysDictList = sysDictService.findAll();
        List<DictResponseVO> dictList = ListUtil.convertList(sysDictList, DictResponseVO.class);
        tokenModel.setDictList(dictList);
        tokenModel.setMenuList(Collections.emptyList());
        //
        return tokenModel;
    }

    /**
     * 退出登录
     *
     * @return
     * @throws Exception
     */
    @Override
    public void logout() throws Exception {
        //TODO 1
    }
}
